﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using SubSonic;
using BookShop;
using System.Collections.Generic;

/// <summary>
/// FlowProcess 的摘要说明
/// </summary>
public class FlowProcess
{
	public FlowProcess()
	{
		//
		// TODO: 在此处添加构造函数逻辑
		//
	}

    /// <summary>
    /// 流程的初始化
    /// </summary>
    /// <param name="flowID">流程号</param>
    /// <param name="source">待处理单号</param>
    /// <param name="sender">发送者</param>
    public void FlowInitalize(string flowID, string source, string sender)
    {
        // 新建一条Task记录
        new TaskController().Insert(flowID, 1, sender, null, null, "new", source, null);

        // 查找该流程第一步的负责岗位、操作名、类型
        // 注：流程的每一步可能涉及多个岗位！
        DataSet ds = new Select(Flow.PrincipalColumn, Flow.OperationColumn, Flow.TypeColumn).From<Flow>()
                            .Where(Flow.FlowIDColumn).IsEqualTo(flowID)
                            .And(Flow.OrderColumn).IsEqualTo(1)
                            .ExecuteDataSet();
        List<String> principalList = new List<string>();
        List<String> titleList = new List<string>();
        List<String> typeList = new List<string>();
        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        {
            string principal = ds.Tables[0].Rows[i]["Principal"].ToString();
            principalList.Add(principal);
            string title = ds.Tables[0].Rows[i]["Operation"].ToString() + "中";
            titleList.Add(title);
            string type = ds.Tables[0].Rows[i]["Type"].ToString();
            typeList.Add(type);
        }

        //根据岗位通知到具体的人
        NoticePrincipal(principalList, titleList, typeList, source, sender);
        
            
    }

    public void FlowExecute(string flowID, string source, string sender)
    {
        // 先查找该流程目前在第几步
        int order;
        order = Convert.ToInt32(new Select(Task.OrderColumn).From<Task>().Where(Task.FlowIDColumn).IsEqualTo(flowID).And(Task.SourceColumn).IsEqualTo(source).ExecuteScalar());

        // 修改该流程的正处于的步骤
        order++;

        // 查找该流程一该步的负责岗位、操作名、类型
        DataSet ds = new Select(Flow.PrincipalColumn, Flow.OperationColumn, Flow.TypeColumn).From<Flow>()
                            .Where(Flow.FlowIDColumn).IsEqualTo(flowID)
                            .And(Flow.OrderColumn).IsEqualTo(order)
                            .ExecuteDataSet();
        List<String> principalList = new List<string>();
        List<String> titleList = new List<string>();
        List<String> typeList = new List<string>();
        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        {
            string principal = ds.Tables[0].Rows[i]["Principal"].ToString();
            principalList.Add(principal);
            string title = ds.Tables[0].Rows[i]["Operation"].ToString() + "中";
            titleList.Add(title);
            string type = ds.Tables[0].Rows[i]["Type"].ToString();
            typeList.Add(type);
        }

        //根据岗位通知到具体的人
        NoticePrincipal(principalList, titleList, typeList, source, sender);
        
    }

    /// <summary>
    /// 获取下一个步骤的URL
    /// </summary>
    /// <param name="source"></param>
    /// <returns></returns>
    public string GetNextDestination(string type,string source, string storeid)
    {
        string url = string.Empty;
        // 根据类型的不同赋予不同的目的url
        switch (type)
        {
            case "补货通知":
                url = "~/AllocationDetailView.aspx?source="+ source +"&storeid=" + storeid;
                break;
            case "补货配送":
                url = "~/TransArrange.aspx";
                break;
            default:
                url = string.Empty;
                break;
        }
        return url;
    }

    private void NoticePrincipal(List<string> principalList, List<String> titleList, List<String>typeList, string source, string sender)
    {
        string type = source.Substring(0, 4);
        switch (type)
        {
            case "BH01":
                for (int i = 0; i < principalList.Count; i++)
                {
                    string positionId;
                    positionId = new Select(Position.PositionIdColumn).From<Position>().Where(Position.PositionNameColumn).IsEqualTo(principalList[i]).ExecuteScalar().ToString();
                    if (positionId.Substring(0, 2) == "00")
                    {
                        string username;
                        username = new Select(Employee.UserNameColumn).From<Employee>().Where(Employee.PositionColumn).IsEqualTo(principalList[i]).ExecuteScalar().ToString();
                        new MessageController().Insert(titleList[i], username, sender, "new", source, DateTime.Now, typeList[i]);
                    }
                    else
                    {
                        DataSet ds = new Select(Employee.UserNameColumn).From<Employee>()
                            .Where(Employee.PositionColumn).IsEqualTo(principalList[i])
                            .And(Employee.StoreIDColumn)
                            .In(new Select(AllocationDetail.StoreIDColumn).From<AllocationDetail>()
                                .Where(AllocationDetail.AllocationIDColumn).IsEqualTo(source))
                            .ExecuteDataSet();
                        for (int j = 0; j < ds.Tables[0].Rows.Count; j++)
                        {
                            new MessageController().Insert(titleList[i], ds.Tables[0].Rows[i]["UserName"].ToString(), sender, "new", source, DateTime.Now, typeList[i]);
                        }
                    }
                }
                break;
            default:
                break;
        }                
    }
}
